package org.grottarossa.ui.core;

import java.util.ArrayList;
import java.util.Hashtable;

import org.grottarossa.template.data.DataType;
import org.grottarossa.template.data.DataTypeValue;
import org.grottarossa.template.data.pojo.users.User;
import org.grottarossa.template.drivers.DriverException;



import com.vaadin.data.Item;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Field;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.OptionGroup;

public class FieldComponent
{
	public static String getKeyFromDataType(ApplicationSession appSession, String fieldKey, String sValue) throws DriverException
	{
		DataType dataType = appSession.getBuilder().getCaseManagement().getFieldsMapConverted().get(fieldKey);

		for(DataTypeValue datatypeVal: dataType.getValues())
		{
			if( ( datatypeVal.getKey()!=null) && (datatypeVal.getValue().equals(sValue) ) )
			{
				return datatypeVal.getKey();
			}
		}
		return null;
	}

	public static Field getRadioButtonList(ApplicationSession appSession, String caption, String fieldKey, String errorMsg) throws DriverException
	{
		OptionGroup radioButtonList = new OptionGroup(caption);

		radioButtonList.setMultiSelect(false);

		radioButtonList.setNullSelectionAllowed(false);
		DataType dataType = appSession.getBuilder().getCaseManagement().getFieldsMapConverted().get(fieldKey);

		radioButtonList.addContainerProperty("key", String.class, null);
		radioButtonList.addContainerProperty("value", Integer.class, null);
		radioButtonList.setItemCaptionPropertyId("key");

		for(DataTypeValue datatypeVal: dataType.getValues())
		{
			if(datatypeVal.getKey()!=null)
			{
				Item addedItem = (Item) radioButtonList.addItem(Integer.parseInt(datatypeVal.getValue()));
				addedItem.getItemProperty("key").setValue(datatypeVal.getKey());
				addedItem.getItemProperty("value").setValue(Integer.parseInt(datatypeVal.getValue()));
			}
		}

		radioButtonList.setRequired(true);
		radioButtonList.setRequiredError(errorMsg);
		return radioButtonList;
	}

	public static Field getInitComboBox(ApplicationSession appSession, String caption,String fieldKey, String errorMsg) throws DriverException
	{
		ComboBox cb = new ComboBox(caption);
		cb.setNullSelectionAllowed(false);
		DataType dataType = appSession.getBuilder().getCaseManagement().getFieldsMapConverted().get(fieldKey);

		cb.addContainerProperty("key", String.class, null);
		cb.addContainerProperty("value", Integer.class, null);
		cb.setItemCaptionPropertyId("key");

		for(DataTypeValue datatypeVal: dataType.getValues())
		{
			if(datatypeVal.getKey()!=null)
			{
				Item addedItem = (Item) cb.addItem(Integer.parseInt(datatypeVal.getValue()));
				addedItem.getItemProperty("key").setValue(datatypeVal.getKey());
				addedItem.getItemProperty("value").setValue(Integer.parseInt(datatypeVal.getValue()));
			}
		}
		cb.setWidth("100%");
		cb.setRequired(false);
		cb.setRequiredError(errorMsg);
		cb.setFilteringMode(ComboBox.FILTERINGMODE_STARTSWITH);
		return cb;
	}

	public static Field getListSelect(String caption, String errorMsg, ArrayList<String> values) throws DriverException
	{
		ListSelect list = new ListSelect(caption);
		list.setNullSelectionAllowed(false);
		list.setWidth("100%");
		list.setRequired(false);
		list.setRequiredError(errorMsg);

		for(String item: values)
		{
			list.addItem(item);
		}

		return list;
	}

	//il metodo è da modificare. Da rendere più generico
	public static Field getAssigneeUsers(ApplicationSession appSession, String caption, ArrayList<String> roles) throws DriverException
	{
		ComboBox cb = new ComboBox(caption);
		cb.addContainerProperty("DISPLAY", String.class, null);
		cb.setItemCaptionPropertyId("DISPLAY");
		cb.setNullSelectionAllowed(false);
		//cb.addContainerProperty("key", String.class, null);
		//cb.setItemCaptionPropertyId("key");
		Hashtable<String, ArrayList<String>> usersToRole = appSession.getBuilder().getPortalManagement().getUser(roles);


		ArrayList<String> settoreUsers = null;
		ArrayList<String> proceduristaUsers = null;
		int i=0;

//		if(usersToRole.containsKey("Responsabile Settore"))
//		{
//			settoreUsers = usersToRole.get("Responsabile Settore");
//
//			for(String suser : settoreUsers)
//			{
//				User user = ECM.getUserManagement().getUser(suser);
//				Item addedItem = (Item) cb.addItem(user.getLoginID());
//				addedItem.getItemProperty("DISPLAY").setValue(user.getFirstName() + " " +user.getLastName());
//				i++;
//			}
//		}
		if(usersToRole.containsKey("Progettista"))
		{
			proceduristaUsers = usersToRole.get("Progettista");
			for (String puser : proceduristaUsers)
			{
				User user = appSession.getBuilder().getUserManagement().getUser(puser);

				Item addedItem2 = (Item) cb.addItem(user.getLoginID());
				addedItem2.getItemProperty("DISPLAY").setValue(user.getFirstName() + " " +user.getLastName());
				i++;
			}
		}
		return cb;
	}


}
